﻿Imports System
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Collections.Generic

Imports DotNetNuke
Imports DotNetNuke.Security
Imports Insurance

Namespace DotNetNuke.Modules.Insurance
    Partial Class InsuranceProvider
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Private Const PrefixDr As String = "Dr. "
        Private totalRecords As Integer = 0

        Private Sub LoadDataForRepeater(ByVal insuranceId As Integer)
            Dim context As InsuranceDataContext = New InsuranceDataContext()

            Dim lstInProvider = (From ip In context.cwc_Insurance_Providers _
                                Join p In context.cwc_Providers On ip.providerID Equals p.providerID _
                                Join i In context.cwc_Insurances On ip.cwc_Insurance.insuranceID Equals i.insuranceID _
                                Join lp In context.cwc_Location_Providers On lp.providerID Equals p.providerID _
                                Join l In context.cwc_Locations On lp.addressID Equals l.addressID _
                                Where ip.insuranceID = insuranceId _
                                Order By l.state, l.city, p.lName _
                                Select New InsuranceProviderDTO With _
                                { _
                                    .City = l.city & "," & l.state, _
                                    .ProviderId = p.providerID, _
                                    .ProviderName = PrefixDr & p.fName & " " & p.lName, _
                                    .Company = i.company, _
                                    .SecondaryMessage = i.secondaryMessage _
                                }).Distinct().ToList()
            lstInProvider = (From p In lstInProvider Order By p.City Descending, p.ProviderName).ToList()
            totalRecords = lstInProvider.Count

            If totalRecords > 0 Then
                ltrHeading.Text = lstInProvider(0).Company

                If Not String.IsNullOrEmpty(lstInProvider(0).SecondaryMessage) Then
                    secondMsg.Visible = True
                    ltrSecondMsg.Text = lstInProvider(0).SecondaryMessage
                End If
            End If

            rptInsuProvider.DataSource = lstInProvider
            rptInsuProvider.DataBind()
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim iid As Integer = 0

            If Not Request.Params("iid") Is Nothing Then
                iid = Integer.Parse(Request.Params("iid"))
                LoadDataForRepeater(iid)
            End If

        End Sub

        Protected Sub rptInsuProvider_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptInsuProvider.ItemDataBound

            If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim insurance As InsuranceProviderDTO = CType(e.Item.DataItem, InsuranceProviderDTO)
                Dim pnlContent As PlaceHolder = CType(e.Item.FindControl("pnlContent"), PlaceHolder)
                Dim strContent As StringBuilder = New StringBuilder()
                If ViewState("city") Is Nothing Then
                    Dim ltrContent As New Literal()
                    strContent.Append("<ul> <lh style='font-size:10pt'>")
                    strContent.Append(insurance.City)
                    strContent.Append("</lh> <li style='list-style-type:disc !important'>")
                    ltrContent.Text = strContent.ToString()
                    pnlContent.Controls.Add(ltrContent)
                    Dim lnkButton As New LinkButton()
                    lnkButton.Attributes.Add("style", "color:#338B85;font-style:italic")
                    lnkButton.ID = "lnkProvider"
                    lnkButton.Text = insurance.ProviderName
                    lnkButton.CommandArgument = insurance.ProviderId
                    lnkButton.Style.Add("font-size", "10pt")
                    pnlContent.Controls.Add(lnkButton)
                    Dim ltrLiClose As New Literal()
                    ltrLiClose.Text = "</li>"
                    pnlContent.Controls.Add(ltrLiClose)
                Else
                    If ViewState("city").ToString() = insurance.City Then
                        Dim ltrContent As New Literal()
                        strContent.Append("<li style='list-style-type:disc !important'>")
                        ltrContent.Text = strContent.ToString()
                        pnlContent.Controls.Add(ltrContent)
                        Dim lnkButton As New LinkButton()
                        lnkButton.Attributes.Add("style", "color:#338B85;font-style:italic")
                        lnkButton.ID = "lnkProvider"
                        lnkButton.Text = insurance.ProviderName
                        lnkButton.Style.Add("font-size", "10pt")
                        lnkButton.CommandArgument = insurance.ProviderId
                        pnlContent.Controls.Add(lnkButton)
                        Dim ltrLiClose As New Literal()
                        ltrLiClose.Text = "</li>"
                        pnlContent.Controls.Add(ltrLiClose)
                    Else
                        Dim ltrContent As New Literal()
                        strContent.Append("</ul><br/><ul> <lh style='font-size:10pt'>")
                        strContent.Append(insurance.City)
                        strContent.Append("</lh> <li style='list-style-type:disc !important'>")
                        ltrContent.Text = strContent.ToString()
                        pnlContent.Controls.Add(ltrContent)
                        Dim lnkButton As New LinkButton()
                        lnkButton.Attributes.Add("style", "color:#338B85;font-style:italic")
                        lnkButton.ID = "lnkProvider"
                        lnkButton.Text = insurance.ProviderName
                        lnkButton.CommandArgument = insurance.ProviderId
                        pnlContent.Controls.Add(lnkButton)
                        lnkButton.Style.Add("font-size", "10pt")
                        Dim ltrLiClose As New Literal()
                        ltrLiClose.Text = "</li>"
                        pnlContent.Controls.Add(ltrLiClose)
                    End If
                End If
                ViewState("city") = insurance.City
                If e.Item.ItemIndex = totalRecords - 1 Then
                    Dim ltrUlClose As New Literal()
                    ltrUlClose.Text = "</ul>"
                    pnlContent.Controls.Add(ltrUlClose)
                End If
            End If
        End Sub

        Protected Sub rptListInsurance_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptInsuProvider.ItemCommand
            'Me.Response.Redirect("Default.aspx?TabId=" + Me.TabId.ToString + "&pid=" + e.CommandArgument.ToString())
            Me.Response.Redirect("/cwcare/Provider.aspx?pid=" + e.CommandArgument.ToString())
        End Sub
    End Class

    Public Class InsuranceProviderDTO
        Private _providerId As Integer
        Public Property ProviderId() As Integer
            Get
                Return _providerId
            End Get
            Set(ByVal value As Integer)
                _providerId = value
            End Set
        End Property
        Private _providerName As String
        Public Property ProviderName() As String
            Get
                Return _providerName
            End Get
            Set(ByVal value As String)
                _providerName = value
            End Set
        End Property

        Private _city As String
        Public Property City() As String
            Get
                Return _city
            End Get
            Set(ByVal value As String)
                _city = value
            End Set
        End Property

        Private _addressId As Integer
        Public Property AddressId() As Integer
            Get
                Return _addressId
            End Get
            Set(ByVal value As Integer)
                _addressId = value
            End Set
        End Property

        Private _company As String
        Public Property Company() As String
            Get
                Return _company
            End Get
            Set(ByVal value As String)
                _company = value
            End Set
        End Property

        Private _secondaryMessage As String
        Public Property SecondaryMessage() As String
            Get
                Return _secondaryMessage
            End Get
            Set(ByVal value As String)
                _secondaryMessage = value
            End Set
        End Property
    End Class
End Namespace
